'use strict';

const BaseService = require('./base');

class ExpressOrderService extends BaseService {
  constructor(ctx) {
    super(ctx, 'ExpressOrder');
  }
  
  // 重写create方法，支持事务
  async create(data, transaction = null) {
    const options = {};
    if (transaction) options.transaction = transaction;
    
    return this.ctx.model.ExpressOrder.create(data, options);
  }
  
  // 重写update方法，支持事务
  async update(id, data, transaction = null) {
    const options = { where: { id } };
    if (transaction) options.transaction = transaction;
    
    return this.ctx.model.ExpressOrder.update(data, options);
  }
  
  // 重写allList方法，添加关联查询
  async allList(where) {
    return this.ctx.model.ExpressOrder.findAll({
      where,
      order: [['id', 'DESC']],
      include: [
        {
          model: this.ctx.model.User,
          as: 'user',
          attributes: ['id', 'nickname', 'phone']
        },
        {
          model: this.ctx.model.LogisticsCompany,
          as: 'logisticsCompany',
          attributes: ['id', 'company_name']
        },
        {
          model: this.ctx.model.Warehouse,
          as: 'warehouse',
          attributes: ['id', 'warehouse_name']
        },
        {
          model: this.ctx.model.ExpressOutlet,
          as: 'outlet',
          attributes: ['id', 'outlet_name']
        },
        {
          model: this.ctx.model.InsuranceCoverage,
          as: 'insuranceCoverage',
          attributes: ['id', 'coverage_name', 'coverage_amount', 'premium']
        }
      ]
    });
  }
  
  // 重写getPage方法，添加关联查询
  async getPage(where, page, pageSize, include = null) {
    const defaultInclude = [
      {
        model: this.ctx.model.User,
        as: 'user',
        attributes: ['id', 'nickname', 'phone']
      },
      {
        model: this.ctx.model.LogisticsCompany,
        as: 'logisticsCompany',
        attributes: ['id', 'company_name']
      },
      {
        model: this.ctx.model.Warehouse,
        as: 'warehouse',
        attributes: ['id', 'warehouse_name']
      },
      {
        model: this.ctx.model.ExpressOutlet,
        as: 'outlet',
        attributes: ['id', 'outlet_name']
      },
      {
        model: this.ctx.model.InsuranceCoverage,
        as: 'insuranceCoverage',
        attributes: ['id', 'coverage_name', 'coverage_amount', 'premium']
      }
    ];
    
    return this.ctx.model.ExpressOrder.findAndCountAll({
      where,
      offset: pageSize * (page - 1),
      limit: pageSize,
      order: [['id', 'DESC']],
      include: include || defaultInclude
    });
  }
  
  // 获取用户订单列表
  async getUserOrders(where) {
    return this.ctx.model.ExpressOrder.findAll({
      where,
      order: [['id', 'DESC']],
      include: [
        {
          model: this.ctx.model.LogisticsCompany,
          as: 'logisticsCompany',
          attributes: ['id', 'company_name']
        },
        {
          model: this.ctx.model.Warehouse,
          as: 'warehouse',
          attributes: ['id', 'warehouse_name']
        },
        {
          model: this.ctx.model.ExpressOutlet,
          as: 'outlet',
          attributes: ['id', 'outlet_name']
        },
        {
          model: this.ctx.model.InsuranceCoverage,
          as: 'insuranceCoverage',
          attributes: ['id', 'coverage_name', 'coverage_amount', 'premium']
        }
      ]
    });
  }
  
  // 获取订单物流轨迹
  async getOrderTrackInfo(orderId) {
    const order = await this.ctx.model.ExpressOrder.findOne({
      where: { id: orderId },
      include: [
        {
          model: this.ctx.model.LogisticsCompany,
          as: 'logisticsCompany',
          attributes: ['id', 'company_name']
        },
        {
          model: this.ctx.model.Warehouse,
          as: 'warehouse',
          attributes: ['id', 'warehouse_name']
        },
        {
          model: this.ctx.model.ExpressOutlet,
          as: 'outlet',
          attributes: ['id', 'outlet_name']
        },
        {
          model: this.ctx.model.OrderItem,
          attributes: ['id', 'item_name', 'quantity', 'weight', 'volume', 'price', 'subtotal']
        },
        {
          model: this.ctx.model.LogisticsTrack,
          attributes: ['id', 'track_status', 'track_info', 'track_location', 'operator', 'create_time'],
          order: [['create_time', 'DESC']]
        }
      ]
    });
    
    return order;
  }
}

module.exports = ExpressOrderService;
